/*
 * @Author: your name
 * @Date: 2021-11-18 09:50:22
 * @LastEditTime: 2021-11-18 10:07:40
 * @LastEditors: your name
 * @Description: 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 * @FilePath: \rtspclient\src\rtcp.cc
 */
#include "rtcp.h"

// RTCP协议信息
std::string RtcpMsg::GetRRMsg() {
    RTCP_HEAD rtcp_head;
    rtcp_head.bit2Version = 2;
    rtcp_head.bit1Padding = 0;
    rtcp_head.bit5PCount  = 0;
    rtcp_head.Ptype       = 201;
    rtcp_head.Length      = ntohs(1);
    rtcp_head.ssrc        = ssrc_;
    std::string str;
    str.resize(sizeof(RTCP_HEAD));
    // str.copy((char*)&rtcp_head, sizeof(RTCP_HEAD), 0);
    memcpy(&str[0], &rtcp_head, sizeof(RTCP_HEAD));
    printf("GetRRMsg-0x%x 0x%x 0x%x 0x%x\n", str[0], str[1], str[2], str[3]);
    return str;
}

std::string RtcpMsg::GetSDESMsg() {
    RTCP_HEAD rtcp_head;
    rtcp_head.bit2Version = 2;
    rtcp_head.bit1Padding = 0;
    rtcp_head.bit5PCount  = 1;
    rtcp_head.Ptype       = 202;
    rtcp_head.Length      = ntohs(sizeof(SDES_ITEMS) / 4 + 1);
    rtcp_head.ssrc        = ssrc_;
    // SDES_ITEMS
    printf("SDES_ITEMS-size:%d\n", sizeof(SDES_ITEMS));
    SDES_ITEMS items;
    items.type = 1;
    memcpy(&items.bit16text, "cl", 2);
    memcpy(items.text, "_items", 2);
    items.len = 8;

    std::string str;
    str.resize(sizeof(RTCP_HEAD) + sizeof(SDES_ITEMS));
    // str.copy((char*)&rtcp_head, sizeof(RTCP_HEAD), 0);
    // str.copy((char*)&items, sizeof(SDES_ITEMS), sizeof(RTCP_HEAD));
    memcpy(&str[0], &rtcp_head, sizeof(RTCP_HEAD));
    memcpy(&str[sizeof(RTCP_HEAD)], &items, sizeof(SDES_ITEMS));

    return str;
}

std::string RtcpMsg::GetBYEMsg(std::string ssrc) {
    RTCP_HEAD rtcp_head;
    rtcp_head.bit2Version = 2;
    rtcp_head.bit1Padding = 0;
    rtcp_head.bit5PCount  = 1;
    rtcp_head.Ptype       = 203;
    rtcp_head.Length      = ntohs(1);
    ;
    rtcp_head.ssrc = (unsigned int)atoi(ssrc.c_str());
    std::string str;
    str.resize(sizeof(RTCP_HEAD));
    memcpy(&str[0], &rtcp_head, sizeof(RTCP_HEAD));

    return str;
}